package com.are.odisea;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class SrvLogin
 */
@WebServlet("/SrvLogin")
public class SrvLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
	

    /**
     * Default constructor. 
     */
    public SrvLogin() {
        // TODO Auto-generated constructor stub
    }
    
  
    protected void ProcesarPeticion(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
    	
    	PrintWriter out = response.getWriter();
    	HttpSession session = request.getSession(true);
    	String usuario = (String)request.getParameter("usuario");
    	String clave = (String)request.getParameter("password");
    	
    	db conexion = null;
    	try {
			conexion = new db();
			
			login validar = new login(conexion);
			validar.setEstado("Sin Iniciar");
			if (validar.validLogin(usuario, clave)) {
				out.print("OK");
				session.setAttribute("usuario", usuario);
				session.setAttribute("id_usuario", validar.getKey());
			}else {
				out.print("Credenciales No Validas");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if (conexion != null){
				try {
					conexion.Close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					//out.print("NO se conecto a la base de datps");
					e.printStackTrace();
				}
			}
		}
    	
    	
    }
    

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.ProcesarPeticion(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.ProcesarPeticion(request, response);
	}

	/*
			String sql = "insert into equipos (imei,marca,estado,tipo) values (?,?,?,?)";
			java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
			pst.setString(1, this.imei);
			pst.setString(2, this.marca);
			pst.setInt(3, this.activo);
			pst.setInt(4, this.tipo);
			if (conexion.Update(pst) > 0) {
	 */
	 
}
